/*
 * Copyright (c) 2010 Marc Poppleton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.marcpoppleton.strawberry;

import org.marcpoppleton.strawberry.provider.CoCManager;
import org.marcpoppleton.strawberry.provider.Coc;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class EditCameraActivity extends Activity implements OnClickListener{
	
    private TextView mCameraName;
    private TextView mCocValue;
    private Button mCancelButton;
    private Button mOkButton;
	
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_camera);
        setupViews();
        Parcelable coc = this.getIntent().getParcelableExtra(Coc.PARCEL_NAME);
        if((null!=coc) && (coc instanceof Coc))setData((Coc)coc);
    }	
    
    public void onClick(View v){
    	if(v.getId() == R.id.ok_button){
    		Coc coc = new Coc();
    		coc.setName(mCameraName.getText().toString());
    		coc.setValue(Float.parseFloat(mCocValue.getText().toString()));
    		CoCManager.addCamera(getContentResolver(), coc);
    		this.finish();
    	}
    	if(v.getId() == R.id.cancel_button){
    		this.finish();
    	}
    }
    
    private void setupViews(){
    	mCancelButton = (Button)findViewById(R.id.ok_button);
    	mOkButton = (Button)findViewById(R.id.cancel_button);
    	mCancelButton.setOnClickListener(this);
    	mOkButton.setOnClickListener(this);
    	mCameraName = (TextView)findViewById(R.id.camera_name);
    	mCocValue = (TextView)findViewById(R.id.camera_coc);
    }
    
    private void setData(Coc coc){
    	mCameraName.setText(coc.getName());
    	mCocValue.setText(coc.getValue().toString());
    }


	/**
	 * Starts the CamerasActivity for the specified user.
	 * 
	 * @param context
	 *            The application's environment.
	 */
	 static void show(Context context,Parcelable parcel) {
		 final Intent intent = new Intent(context, EditCameraActivity.class);
		 intent.putExtra(Coc.PARCEL_NAME,parcel);
		 context.startActivity(intent);
	 }

}
